package com.microservice.system.controller;

import com.microservice.core.domain.AjaxResult;
import com.microservice.model.system.User;
import com.microservice.system.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 用户服务控制器
 */
@RestController
@RequestMapping("/user")
public class UserServiceController {

    @Resource
    private IUserService userService;

    @PostMapping("/add")
    public AjaxResult addUser(@RequestBody User user) {
        return userService.add(user);
    }

    @GetMapping("/getPermissionByUsername")
    public AjaxResult getPermissionByUsername(@RequestParam("username") String username) {
        return userService.getPermissionByUsername(username);
    }

    @GetMapping("/getRoleByUsername")
    public AjaxResult getRoleByUsername(@RequestParam("username") String username) {
        return userService.getRoleByUsername(username);
    }

    @GetMapping("/getUserByUsername")
    public AjaxResult getUserByUsername(@RequestParam("username") String username) {
        return userService.getUserByUsername(username);
    }

    @GetMapping("/userList")
    public AjaxResult getUserList() {
        return userService.getUserList();
    }

    @PreAuthorize("hasRole('ROLE_ADMIN') and hasAuthority('/user/delete')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody String id) {
        System.out.println("id为："+id);
        return userService.delete(id);
    }
}
